package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;
import com.itboye.eweilisten.util.widget.LineEditText;

import java.util.List;

/**
 * 单词听写
 */
public class TingXieAdapter extends RecyclerView.Adapter<TingXieAdapter.MyViewHolder> {
    Context context;
    private List<TIbean> beans;

    String trues;

    public TingXieAdapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.eweitingtext, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // holder.danxuan_title.setText(beans.get(position).getTitle());
        String.valueOf(beans.get(position).getAnwser_true());
        for (int j = 0; j < beans.get(position).getContent().size(); j++) {
            questionContentAdd(holder.answerLine, beans.get(position).getContent().get(j), beans.get(position).getDt_type(),position);
        }
        for (int j = 0; j < holder.answerLine.getChildCount(); j++) {
            if ("null".equals(holder.answerLine.getChildAt(j).getTag(R.id.selected) + "")) {

            } else {
                if (0 == Integer.parseInt(holder.answerLine.getChildAt(j).getTag(R.id.selected) + "")) {
                    // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                    holder.answerLine.getChildAt(j).setBackgroundColor(Color.parseColor("#3b7830"));
                    holder.answerLine.getChildAt(j).setTag(R.id.selected, 2);
                    break;
                }
            }
        }
    }

    /**
     * 内容
     *
     * @param questionLine
     * @param string
     */
    private void questionContentAdd(final FlowLayout questionLine, String string, final String type,final int position) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
//        params.setMargins(0, 5, 0, 5);
        if ("_".equals(string)) {
            LineEditText editText = new LineEditText(context);
            if (type.equals("6233")) {
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
            }

            editText.setMinWidth(40);
            editText.setTextSize(16);
            editText.setTag(R.id.selected, 0);
            editText.setPadding(15, 0, 15, 0);
            editText.setBackgroundColor(Color.parseColor("#ffffff"));
            editText.setCursorVisible(false);
            editText.setLayoutParams(params);
            questionLine.addView(editText);
            editText.setTag(false);
            //点击事件
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // 这个要分两个方法去做
                    if ("".equals(((TextView) v).getText() + "")) {
                        // 无填好的内容
                        for (int i = 0; i < questionLine.getChildCount(); i++) {
                            // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                            if ("2".equals(questionLine.getChildAt(i).getTag(R.id.selected) + "")) {
                                questionLine.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff" + ""));
                                questionLine.getChildAt(i).setTag(R.id.selected, 0);
                                questionLine.getChildAt(i).setTag(false);
                                break;
                            }
                        }
                        ((LineEditText) v).setBackgroundColor(Color.parseColor("#3b7830"));
                        // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                        ((LineEditText) v).setTag(R.id.selected, 2);
                        v.setTag(true);

                    } else {
                        // 有填好的内容
                        for (int i = 0; i < questionLine.getChildCount(); i++) {

                            // tag标记位 0 无数据未选中 1有数据未选中 2无数据选中 3有数据选中
                            if ("2".equals(questionLine.getChildAt(i).getTag(R.id.selected) + "")) {
                                questionLine.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff" + ""));
                                questionLine.getChildAt(i).setTag(R.id.selected, 0);
                            }
                        }
                        ((LineEditText) v).setBackgroundColor(Color.parseColor("#3b7830"));
                        // tag标记位 0 无数据未选中 1有数据未选-中 2无数据选中 3有数据选中
                        ((LineEditText) v).setTag(R.id.selected, 2);
                    }

                }
            });
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < questionLine.getChildCount(); i++) {
                        if (questionLine.getChildAt(i) instanceof EditText) {
                            String to = ((EditText) questionLine.getChildAt(i)).getText().toString();
                            sb.append(to);
                        }
                    }
                    StringBuilder ssss= new StringBuilder();
                    for(int j=0;j<beans.get(position).getAnwser_true().size();j++){
                        ssss.append(beans.get(position).getAnwser_true().get(j));
                    }
                    System.out.println("UIIHIUHHOIH"+sb);
                    System.out.println("UIIHIUHHOIH"+ssss);
                    if (sb.equals("")){
                        beans.get(position).setXuan(2);
                    }else if (sb.toString().equals(ssss.toString())){
                        beans.get(position).setXuan(0);//0代表选择正确
                    }else {
                        beans.get(position).setXuan(1);
                    }

                    if (type.equals("6233")) {
                        if (s.length() >= 1) {
                            int index = 0;
                            for (int i1 = 0; i1 < questionLine.getChildCount(); i1++) {
                                questionLine.getChildAt(i1).setBackgroundColor(Color.parseColor("#ffffff" + ""));
                                if (Boolean.valueOf(questionLine.getChildAt(i1).getTag() + "")) {
                                    index = i1;
                                }
                                questionLine.getChildAt(i1).setTag(false);
                                if (index == questionLine.getChildCount() - 1) {
                                    index = -1;
                                }
                            }
                            for (int i = index; i < questionLine.getChildCount(); i++) {
                                index++;
                                if (questionLine.getChildAt(index) instanceof EditText) {
                                    questionLine.getChildAt(index).setFocusable(true);
                                    questionLine.getChildAt(index).setTag(true);
                                    questionLine.getChildAt(index).setFocusableInTouchMode(true);
                                    questionLine.getChildAt(index).requestFocus();
                                    questionLine.getChildAt(index).setBackgroundColor(Color.parseColor("#3b7830" + ""));
                                    break;
                                }
                            }
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });

        } else {
            TextView textView = new TextView(context);
            textView.setText(string);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        }

    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView danxuan_title;
        public FlowLayout answerLine;

        public MyViewHolder(View itemView) {
            super(itemView);
            danxuan_title = (TextView) itemView.findViewById(R.id.danxuan_title);
            answerLine = (FlowLayout) itemView.findViewById(R.id.answerLine);
        }
    }
}
